﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CSharpTestApp
{
     public class FileDirManager
    {
        string[] filter =
        {
            "avcodec-57",
            "avdevice-57",
            "avfilter-6",
            "avformat-57",
            "avutil-55",
            "freetype6",
            "HardCursor",
            "libfastlz",
            "libfastlz",
            "liblz4",
            "liblzma",
            "libzipw",
            "Lua51",
            "NdLibCef",
            "OculusSpatializerPlugin",
            "OVRLipSync",
            "PomeloClientNet",
            "postproc-54",
            "RapidXml",
            "recast_x64",
            "SQLite.Interop",
            "sqlite3",
            "swresample-2",
            "swscale-4",
            "HDAvatarEditer",
        };        


        public void FindDllReferences(string sourceDir, string outFilePath)
        {
            List<string> listFilter = new List<string>(filter);
            try
            {
                if (!Directory.Exists(sourceDir))
                {
                    return;
                }
                File.Delete(outFilePath);
                string[] files = Directory.GetFiles(sourceDir, "*.dll", SearchOption.AllDirectories);
                int index = sourceDir.Length;
                foreach (string sourceFileName in files)
                {
                    string filename = Path.GetFileNameWithoutExtension(sourceFileName);

                    if (listFilter.IndexOf(filename) != -1)
                    {
                        continue;
                    }
                    File.AppendAllText(outFilePath, "\r\n");
                    File.AppendAllText(outFilePath, "DLL NAME: " + sourceFileName + "\r\n");
                    Assembly ass = Assembly.LoadFile(sourceFileName);
                    var refNames = ass.GetReferencedAssemblies();
                    foreach (var item in refNames)
                    {                   
                        File.AppendAllText(outFilePath,item.FullName + "\r\n");
                    }
                }

            }
            catch (Exception ex)
            { 
                string strMsg = string.Format("CopyDirFiles failed :{0}", ex.ToString());
            }

        }

    }
}
